package com.tourlaid.mamagerment.ao.pc.impl

import com.github.pagehelper.Page
import com.github.pagehelper.PageHelper
import com.tourlaid.mamagerment.ao.pc.StockInAo
import com.tourlaid.mamagerment.entity.ResponseData
import com.tourlaid.mamagerment.entity.StockIn
import com.tourlaid.mamagerment.entity.StockInVo
import com.tourlaid.mamagerment.form.StockInForm
import com.tourlaid.mamagerment.service.GoodsService
import com.tourlaid.mamagerment.service.StockInService
import com.tourlaid.mamagerment.service.SupplierService
import com.tourlaid.mamagerment.util.DateUtil
import com.tourlaid.mamagerment.vo.StockInListVo
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/23.
 */
@Service("stockInAo")
class StockInAoImpl : StockInAo {
    override fun getStock(stockInId: Long): Int {
        var stockIn = stockInService!!.getById(stockInId);
        return stockIn?.stock ?: 0
    }

    @Resource
    var stockInService: StockInService? = null
    @Resource
    var goodsService: GoodsService? = null
    @Resource
    var supplierService: SupplierService? = null

    override fun list(pageNo: Int, pageSize: Int): ResponseData<ArrayList<StockInListVo>> {
        var voList = ArrayList<StockInListVo>()
        var responseData = ResponseData(voList)
        PageHelper.startPage<StockIn>(pageNo, pageSize, true)
        var page = stockInService!!.queryForList()!! as Page<StockIn>
        responseData.totalCount = page.getTotal()
        val stockInList = page.getResult()
        if (!CollectionUtils.isEmpty(stockInList)) {
            val goodsIdList = stockInList.map { stockIn -> stockIn.goodsId }
            val supplierIdList = stockInList.map { stockIn -> stockIn.supplierId }
            val goodsList = goodsService!!.queryByIdList(goodsIdList)
            val supplierList = supplierService!!.queryByIdList(supplierIdList)
            val goodsMap = goodsList.associate { Pair(it.id, it.goodsName) }
            val supplierMap = supplierList.associate { Pair(it.id, it.supplierName) }
            for (stockIn in stockInList) {
                var vo = StockInListVo()
                BeanUtils.copyProperties(stockIn, vo)
                vo.goodsName = goodsMap.get(stockIn.goodsId)
                vo.supplierName = supplierMap.get(stockIn.supplierId)
                vo.createTime = DateUtil.getFormantDate(stockIn.createTime, DateUtil.DATA_FORMANT)
                voList.add(vo)
            }
        }
        return responseData
    }

    override fun insert(goodsId: Long?, supplierId: Long?, controlNo: String?, stock: Int?) {
        stockInService!!.insert(controlNo, goodsId, supplierId, stock)
    }

    override fun detail(stockInId: Long): StockInVo? {
        var vo = StockInVo()
        var stockIn = stockInService!!.getById(stockInId)
        stockIn ?: return vo
        BeanUtils.copyProperties(stockIn, vo)
        return vo
    }

    override fun update(form: StockInForm) {
        stockInService!!.update(form)
    }

    override fun delete(stockInId: Long?) {
        stockInService!!.delete(stockInId)
    }
}